import {
  ADD_COUNT,
  ADD_TO_CART,
  CHECK_CLICK
} from "./mutation-types.js"

export default {
  checkClick(context, payload){
    let oldProduct = context.state.cartList.find(item => item.iid == payload.iid)
    
    if(oldProduct){
    context.commit(CHECK_CLICK,oldProduct)
    }
  },
  addCart(context, payload){
return new Promise((resolve, reject) =>{
	// 在数值中查找是否有相同iid
	let oldProduct = context.state.cartList.find(item => item.iid == payload.iid)
	
	// 判断是否找到相同iid
	if(oldProduct){ 
	  // 相同iid.count+1
	  // oldProduct.count += 1
	  context.commit(ADD_COUNT,oldProduct)
		resolve("当前商品数量+1")
	}else{
	  // 未找到则将新数据的count设为1
	  payload.count = 1
	  // 将数据push进去购物车
	  // context.state.cartList.push(payload)
	  context.commit(ADD_TO_CART,payload)
		resolve("添加购物车成功")
	}
})
  }
}